package com.yfuxdeer.youchat.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.yfuxdeer.youchat.dao.FriendDao;
import com.yfuxdeer.youchat.dao.MomentDao;
import com.yfuxdeer.youchat.dao.impl.FriendDaoImpl;
import com.yfuxdeer.youchat.dao.impl.MomentDaoImpl;
import com.yfuxdeer.youchat.pojo.FriendItem;
import com.yfuxdeer.youchat.pojo.Moment;
import com.yfuxdeer.youchat.pojo.MomentItem;
import com.yfuxdeer.youchat.utils.Store;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MomentService extends Service {

    private static final String TAG = MomentService.class.getSimpleName();
    private MomentDao momentDao = new MomentDaoImpl();
    private FriendDao friendDao = new FriendDaoImpl();
    private Store store = Store.getInstance();
    private Bundle bundle;
    private Bundle data;

    public enum METHOD {
        FINDALL, PUBLISH, FINDLAST
    }

    public enum RESULT {
        SUCCESS, NETWORK_EXCEPTION
    }

    public List<MomentItem> findAll() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String hostId = bundle.getString("hostId");
                    Log.e(TAG, "run: hostId: " + hostId);
                    List<MomentItem> momentItemList = new ArrayList<>();
                    //查询好友的
                    List<FriendItem> friendItems = friendDao.findAll(hostId);
                    for (FriendItem friendItem : friendItems) {
                        List<Moment> moments = momentDao.findAll(friendItem.getFriend_id());
                        for (Moment moment : moments) {
                            MomentItem momentItem = new MomentItem(
                                    friendItem.getFriend_id(),
                                    friendItem.getRemark(),
                                    moment.getTime(),
                                    moment.getContent()
                            );
                            momentItemList.add(momentItem);
                        }
                    }
                    //查询自己的
                    List<Moment> moments = momentDao.findAll(hostId);
                    for (Moment moment : moments) {
                        MomentItem momentItem = new MomentItem(
                                store.getCurrentUser().getUsername(),
                                store.getCurrentUser().getNickName(),
                                moment.getTime(),
                                moment.getContent()
                        );
                        momentItemList.add(momentItem);
                    }
                    //按照时间排序
                    Collections.sort(momentItemList, new Comparator<MomentItem>() {
                        @Override
                        public int compare(MomentItem momentItem, MomentItem t1) {
                            return t1.getTime().compareTo(momentItem.getTime());
                        }
                    });
                    Log.e(TAG, "run: momentItemList.size() : " + momentItemList.size());
                    data.putSerializable("momentItemList", (Serializable) momentItemList);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.findAllMomentReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
        return null;
    }

    public void publishMoment() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Moment moment = (Moment) bundle.getSerializable("moment");
                    momentDao.insert(moment);
                    data.putSerializable("moment", moment);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.publishMomentReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    public void findLastMomentById() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String username = bundle.getString("username");
                    Moment moment = momentDao.findLastById(username);
                    data.putSerializable("moment", moment);
                    data.putSerializable("result", RESULT.SUCCESS);
                } catch (Exception e) {
                    data.putSerializable("result", RESULT.NETWORK_EXCEPTION);
                }
                Intent broadcast = new Intent("com.yfuxdeer.youchat.findLastMomentReceiver");
                broadcast.putExtras(data);
                sendBroadcast(broadcast);
            }
        }).start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: " + "接收到服务请求");
        bundle = intent.getExtras();
        data = new Bundle();
        METHOD method = (METHOD) bundle.getSerializable("method");
        switch (method) {
            case FINDALL:
                findAll();
                break;
            case PUBLISH:
                publishMoment();
                break;
            case FINDLAST:
                findLastMomentById();
                break;
        }
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }
}
